App.controller('startController', ['$scope', '$state', '$stateParams', 'Page', 'Form', function($scope, $state, $stateParams, Page, Form) {

    $scope.process = $stateParams.process;
    $scope.businessId = $stateParams.businessId;
    $scope._service = $stateParams._service;
    
    $scope.properties = [];
    $scope._properties = {};
    $scope.$watch('properties', function (newValue, oldValue) {
        if (newValue && newValue.length > 0) {
            $scope._properties = {};
            $(newValue).each(function(index, property) {
                $scope._properties[property.id] = property;
            });
        }
    });

    $scope.cancel = function() {// 返回
    	 window.history.back();
    };

    if ($scope.process) {// 启动流程
    	
        $scope.task = {};
        var params = {businessId: $scope.businessId, _service: $scope._service};
        Page.ajaxGet('bpm/process/' + $scope.process.key + '/start-form.do', params, function(resp) {
        	$scope.process = resp.data.processDefinition;
        	$scope.processPath = 'views/bpm/diagram-viewer/index.html?processDefinitionId=' + $scope.process.id;
            $scope.properties = resp.data.properties || [];
            $scope.key = resp.data.key;
            $scope.task.form = resp.data.form || {};
            $scope.buttons = [{
                name: '提交'
            }];
        });

        // 按钮事件
        $scope.approval = function() {
        	var formData = $scope.task.form;
        	var formData2 = Form.toJson('#properties-form');
        	for(var key in formData2) {
                if(formData[key] == undefined) {
                	formData[key] = formData2[key];
                }
            }
        	
        	var data = {};
        	for (var key in formData ) {
        		var temp = $scope._properties[key];
        		if(temp != undefined && temp.readable === true && temp.writable === true) {
        			data[key] = formData[key];
        		}else{
        			data[key] = formData[key];
        		}
        	}
        	
        	data._service = $scope._service || data._service;
        	data.businessId = $scope.businessId || data.businessId;

            Page.ajaxPost('bpm/process/' + $scope.process.id + '/start.do', {properties: data}, function(resp) {
                Page.alert("发起成功", function() {
                    window.history.back();
                });
            });
        }
    }  else {
        window.history.back();
        return;
    }

}]);

App.directive('edBpmInput', ['$parse', '$compile', function ($parse, $compile) {
    return {
        restrict: 'A',
        controller: function($scope, $element, $rootScope){
        },

        link: function (scope, elem, attrs) {

            var model = attrs.ngModel;
            var index = model.lastIndexOf('.');
            model = model.substring(index + 1, model.length);
            $(elem).attr("name", model);

            scope.$watch('_properties', function (newValue, oldValue) {
                if (newValue) {
                    var prop = newValue[model];
                    if (prop) {
                    	//规定:可读并且必须时才是readonly
                        if (prop.readable === true && prop.required ===true) {
                            $(elem).attr("readonly","readonly");
                            return;
                        }
                    }
                }
            });
        }
    };
}]);
